<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container" :inline="true">
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="6" >
            <el-form-item  label="二级仓库名:"   label-width="120px"  label-position="left"  :required="true"  prop="roomName" >
              <el-input v-model="postForm.roomName"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6" >
            <el-form-item  label=" 别  称 :"   label-width="120px"  label-position="left"  :required="true"  prop="shortName" >
              <el-input  v-model="postForm.shortName"  :maxlength="50"   />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6" >
            <el-form-item  label="联系人姓名:"  label-width="120px"  label-position="left"  prop="contactName"  >
              <el-input v-model="postForm.contactName"  :maxlength="250"   />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="6">
            <el-form-item label="联系人电话:"   label-width="120px"  label-position="left"   prop="contactPhone">
              <el-input v-model="postForm.contactPhone" :maxlength="50"     />
            </el-form-item>
          </el-col>
        </el-row>


      </div>
    </el-form>

  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { validateURL } from '@/utils/validate'
import { userSearch } from '@/api/remoteSearch'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/components/Dropdown'
import {roomDetail, updateRoom} from "../../../api/room";

const defaultForm = {
  uid: 0,  // userId
  sid: '', // 一级仓库ID号,storageId
  rid: '', // 二级仓库ID号,roomId
  storageName: '', // 用户名,userId
  position: '', // 用户手机号码
  actionPassword: '', // 当前操作用户的操作密码
  writeValue1: 1,
  writeValue2 : 1,
  writeValue3: 1,
  writeValue4: 0,
  writeValue5: 0,
  writeValue6: 0,
  readValue1: 1,
  readValue2: 1,
  readValue3: 1,
  readValue4: 1,
  readValue5: 1,
  readValue6: 1
}

export default {
  name: 'RoomEdit',
  components: { Tinymce, MDinput, Upload, Sticky,   CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        if(rule.field == 'name'){
          callback(new Error('用户名为必传项！'))
        }
        // if(rule.field == 'customerCode'){
        //   callback(new Error('编码为必传项！'))
        // }
      } else {
        callback()
      }
    }

    return {
      postForm: Object.assign({}, defaultForm),
      editForm:{
        uid: 0,  // userId
        sid: '', // 一级仓库ID号,storageId
        rid: '', // 二级仓库ID号,roomId
        storageName: '', // 用户名,userId
        position: '', // 用户手机号码
        actionPassword: '', // 当前操作用户的操作密码
        writeValue1: 1,
        writeValue2 : 1,
        writeValue3: 1,
        writeValue4: 0,
        writeValue5: 0,
        writeValue6: 0,
        readValue1: 1,
        readValue2: 1,
        readValue3: 1,
        readValue4: 1,
        readValue5: 1,
        readValue6: 1
      },
      loading: false,
      userListOptions: [],
      title:'',
      rules: {
        customerName: [{ validator: validateRequire }],
        customerCode: [{ validator: validateRequire }]
      }
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content_short.length
    }
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.query && this.$route.query.id
      this.roomDetail( id )
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },
  methods: {
    roomDetail(id) {
      roomDetail({id:id}).then(response => {
        this.postForm = response.data.data
        this.title += '  二级仓库 Id:${this.postForm.id}'
        console.log(this.postForm)
      }).catch(err => {
        console.log(err)
      })
    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.doSubmitForm()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    doSubmitForm() {
      this.editForm.id = this.postForm.id
      this.editForm.name = this.postForm.roomName
      this.editForm.shortName = this.postForm.shortName
      this.editForm.contactPhone = this.postForm.contactPhone
      this.editForm.desc = this.postForm.roomDesc
      this.editForm.contactName = this.postForm.contactName
      this.editForm.status = 1
      updateRoom(this.editForm).then(response => {
        if(response.data.code == 0){
          this.$message({
            message:  '保存成功!',
            type: 'success'
          })
        }else{
          this.$message({
            message:  '保存失败!',
            type: 'error'
          })
        }
        // this.list = response.data.items
        // this.total = response.data.total
        this.listLoading = false
      })
    },
    getRemoteUserList(query) {
      userSearch(query).then(response => {
        if (!response.data.items) return
        this.userListOptions = response.data.items.map(v => v.name)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
